DAY5:Delete occurrences of an element if it occurs more than n times


Posted by birdbirdmurmur on 2023-07-18

題目連結:

Delete occurrences of an element if it occurs more than n times

解題思維:

  1. 要有一個儲存數字出現次數的「物件」
  2. 對arr遍歷篩選
  3. 給物件一個屬性
  4. 數字重複就+1 沒有就從1開始
  5. 直到n回傳

實作解法:

function deleteNth(arr,n){
  let count = {};
   return arr.filter( num =>{
     count[num] = (count[num] || 0) +1
     return count[num] <= n
})
}

心得

這題真的超難!
爬完discourse只稍微整理出一點邏輯
我想我目前的程度還不足以挑戰6kyu等級......
連續兩天在6kyu花了太多時間
接下來還是先回頭到7kyu好了


#javascript #Codewars #filter #object







Related Posts

匯入小工具 (3) - 設定環境變數,才不會被看光光

匯入小工具 (3) - 設定環境變數,才不會被看光光

簡易部署 AWS EC2 遠端主機 + Ubuntu LAMP 環境 + phpmyadmin +FileZilla上傳檔案 +遇到問題

簡易部署 AWS EC2 遠端主機 + Ubuntu LAMP 環境 + phpmyadmin +FileZilla上傳檔案 +遇到問題

Web開發學習筆記16 — OOP(Object Oriented Programming)、Constructor Function、Class

Web開發學習筆記16 — OOP(Object Oriented Programming)、Constructor Function、Class


Comments